/**
 * Copyright (c) 2016,http://www.365wuliu.com/  All Rights Reserved.
 */
package com.cargo.base.utils;

import org.apache.log4j.Logger;

import java.io.*;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * PropertiesUtil:常量管理工具类
 * 
 * @Project Name:cargo
 * @File Name:PropertiesUtil.java
 * @Package Name:com.cargo.utils
 * @Creator:wizard
 * @Date:2016年4月14日下午5:51:14
 */
public class PropertiesUtil {

	private PropertiesUtil() {
	}

	private static Properties propertie = new Properties();
	private String[] fileNames;

	protected Logger logger = Logger.getLogger(getClass());

	/**
	 * @Title: load @param @throws Exception 设定文件 @return void 返回类型 @throws
	 */
	public synchronized void load() throws Exception {
		Properties temppropertie = new Properties();
		if (fileNames.length == 0) {
			return;
		}
		InputStream inStream = null;
		for (String filename : fileNames) {
			inStream = PropertiesUtil.class.getResourceAsStream("/" + filename);
			BufferedReader bf = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
			temppropertie.load(bf);
		}
		ClassLoader classloader = getClass().getClassLoader();

		String ss = temppropertie.getProperty("constantsRoot");

		URL aa = classloader.getResource(temppropertie.getProperty("constantsRoot"));

		URI url = classloader.getResource(temppropertie.getProperty("constantsRoot")).toURI();

		File constantsDir = new File(classloader.getResource(temppropertie.getProperty("constantsRoot")).toURI());

		for (File file : getFiles(constantsDir)) {
			inStream = new FileInputStream(file);
			BufferedReader bf = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
			temppropertie.load(bf);
		}
		inStream.close();
		PropertiesUtil.setPropertie(temppropertie);
	}

	/**
	 * getFiles:(这里用一句话描述这个方法的作用).
	 * 
	 * @param constantsDir
	 * @return
	 * @return :Object
	 * @Creator:wizard
	 * @Date:2016年4月18日 下午1:15:45
	 */
	private List<File> getFiles(File constantsDir) {
		List<File> result = new ArrayList<File>();
		if (constantsDir == null) {
		} else {
			File[] files = constantsDir.listFiles();
			if (files != null) {
				for (File f : files) {
					if (f.isDirectory()) {
						result.addAll(getFiles(f));
					} else {
						result.add(f);
					}
				}
			}
		}
		return result;
	}

	/**
	 * 
	 * getValue:通过key获取属性文件的值 @param String key @return
	 * 
	 * @param key
	 * @return String
	 * @Creator:wizard
	 * @Date:2016年4月14日 下午5:52:02
	 */
	public static String getValue(String key) {
		if (key == null || "".equals(key.trim())) {
			return "";
		}
		if (propertie.containsKey(key)) {
			return propertie.getProperty(key);// 得到某一属性的值
		} else {
			return "";
		}
	}

	public void setFileNames(String[] fileNames) {
		this.fileNames = fileNames;
	}

	public static void setPropertie(Properties propertie) {
		PropertiesUtil.propertie = propertie;
	}

}
